//
//  ViewController.m
//  美食百科
//
//  Created by dllo on 16/1/23.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "NewWorking.h"
#import "CarouselModel.h"
#import "Reachability.h"
#import "CarouselView.h"
#import "DrawerView.h"
#import "ClassifyViewController.h"
#import "CookingViewController.h"
#import "SeasonViewController.h"
#import "InforViewController.h"
#import "ScrollView.h"
#import "TopicViewController.h"
#import "MoreViewController.h"
#import "SearchViewController.h"
#import "MovieViewController.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
@interface ViewController ()<UIScrollViewDelegate,CarouselViewDelegate>
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)NSMutableArray *carouselArray;
@property(nonatomic, retain) NSTimer *timer;
@property(nonatomic, assign) BOOL isDragging;
@property(nonatomic, retain) UIPageControl *page;
@property(nonatomic,retain)DrawerView *drawerView;
@property(nonatomic,retain)NSArray *kindArray;
@property(nonatomic,retain)NSArray *itemArray;
@property(nonatomic,retain)NSMutableArray *carouseArray;
@property(nonatomic,retain)UIView *shelterView;
@property(nonatomic,retain)CarouselView *carouselView;
@end

@implementation ViewController
- (void)dealloc
{
    [_scrollView release];
    [_carouselArray release];
    [_timer release];
    [_page release];
    [_drawerView release];
    [_kindArray release];
    [_itemArray release];
    [_carouseArray release];
    [_shelterView release];
    [_carouselView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"🍎%@",NSHomeDirectory());
    // Do any additional setup after loading the view.
   // self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.translucent = NO;
    //self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
    //self.navigationController.navigationBar.alpha = 0;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        Reachability *reach = [Reachability reachabilityForInternetConnection];
        
        //没有网络,显示动画
        if ( 0 == reach.currentReachabilityStatus){
            
            UILabel *nonconnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, kWidth, 50)];
            nonconnectionLabel.textAlignment = NSTextAlignmentCenter;
            nonconnectionLabel.backgroundColor = [UIColor redColor];
            nonconnectionLabel.text = @"网络不给力,刷新试试~";
            nonconnectionLabel.textColor = [UIColor whiteColor];
            [self.view addSubview:nonconnectionLabel];
            
        
            [UIView animateWithDuration:0.6 animations:^{
                nonconnectionLabel.frame = CGRectMake(0, 0, kWidth, 50);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.6 delay:3 options:UIViewAnimationOptionAutoreverse animations:^{
                    nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                    [nonconnectionLabel release];
                    
                } completion:^(BOOL finished) {
                    
                }];
                
                //            [UIView animateWithDuration:1.9 animations:^{
                //                nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                //            //[nonconnectionLabel removeFromSuperview];
                //                [nonconnectionLabel release];
                //            }];
                
            }];
            
        }
    });
    
    
    
    //给self.view添加抽屉手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];
    
    [swipe release];
    
    
    self.kindArray = @[@"group",@"cooking",@"brand",@"restaurant",@"season"];
 self.itemArray =  @[@"类别",@"烹饪方式",@"品牌",@"连锁餐饮",@"季节"];
    //左上角按钮显示抽屉
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(15, 25, 20, 20);
    [leftButton setBackgroundImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(viewAppearance:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    /*************** 解析轮播图数据 ****************/
    
    self.carouselArray = [NSMutableArray array];
    
    [NewWorking GetDayaWithURL:@"http://food.boohee.com/fb/v1/welcome" dic:nil sussess:^(id responseObject) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

        CarouselModel *carouselModel = [CarouselModel carouselModelWithDictionary:responseObject[@"banner"]];
        
        [self.carouselArray addObject:carouselModel];
        
        for (NSDictionary *dic in responseObject[@"others"]) {
            CarouselModel *carouselModel = [CarouselModel carouselModelWithDictionary:dic];
            
            [self.carouselArray addObject:carouselModel];
        }
        
//        NSLog(@"%@",self.carouselArray);
//        NSLog(@"%ld",self.carouselArray.count);
        self.carouselView = [[CarouselView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/2) array:self.carouselArray];
        self.carouselView.delegate = self;
        [self.view addSubview:self.carouselView];
        [_carouselView release];
        
        //搜索
        UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        searchButton.frame = CGRectMake(60, kHeight/2-72, kWidth-120, 37);
        [searchButton setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
        [searchButton addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:searchButton];
        
        //遮罩视图
        self.shelterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [self.shelterView addGestureRecognizer:tap];
        [tap release];
        
        //向左收回抽屉
        UISwipeGestureRecognizer *swipeOne = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
        swipeOne.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.shelterView addGestureRecognizer:swipeOne];
        [swipeOne release];
        [self.view addSubview:self.shelterView];
        self.drawerView = [[DrawerView alloc]initWithFrame:CGRectMake(-kWidth / 5 * 4, 0, kWidth / 5 * 4, kHeight)];
        [self.view addSubview:self.drawerView];
        [_drawerView release];
        
        [self.view bringSubviewToFront:leftButton];
    } filed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
    //专题视图
   self.carouseArray = [NSMutableArray array];
    
    [NewWorking GetDayaWithURL:@"http://food.boohee.com/fb/v1/topics" dic:nil sussess:^(id responseObject) {
        
        
        for (NSDictionary *dic in responseObject[@"topics"]) {
            
            CarouselModel *carouse = [CarouselModel carouselModelWithDictionary:dic];
//            [carouse setValue:dic[@"id"]  forKey:@"Id"];
//            [carouse setValue:dic[@"image_url"]  forKey:@"image_url"];
//            [carouse setValue:dic[@"title"]  forKey:@"title"];
//            [carouse setValue:dic[@"sub_title"]  forKey:@"sub_title"];
//            [carouse setValue:dic[@"page_count"]  forKey:@"page_count"];

            [self.carouseArray addObject:carouse];
            
        }
        
        [self createCarouseView:self.carouseArray];
//        NSLog(@"🍎%@",self.carouseArray);
       
    } filed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}

//搜索界面
-(void)searchAction:(UIButton *)button
{
    SearchViewController *search = [[SearchViewController alloc]init];
    
    [self.navigationController pushViewController:search animated:YES];
    
    [search release];
}

//轮播图跳转详情页
-(void)getTag:(NSInteger)tag
{
    InforViewController *infor = [[InforViewController alloc]init];
    
    infor.number = 1;
    CarouselModel *carouselModel = self.carouselArray[tag];
    infor.name = carouselModel.food_code;
    infor.left = carouselModel.food_name;

    [self.navigationController pushViewController:infor animated:YES];
    
    [infor release];
}
//-(void)viewDidDisappear:(BOOL)animated
//{
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"stop" object:nil];
//}
-(void)viewDidAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    
}
-(void)viewAppearance:(UIButton *)button
{

   
        
        [self.view insertSubview:self.shelterView aboveSubview:self.drawerView];
        self.shelterView.frame = self.view.frame;
        
    
            [UIView animateWithDuration:0.5 animations:^{
            [self.view addSubview:self.drawerView];
                self.drawerView.frame = CGRectMake(0, 0,kWidth / 5 * 4, kHeight);
                NSLog(@"ne");
            }];
    
    
    
    
    
    //抽屉跳转界面
    __block ViewController *viewController = self;
    self.drawerView.block = ^(NSIndexPath *indexPath){
        if (indexPath.row !=1&&indexPath.row !=4&&indexPath.row!=5&&indexPath.row!=6) {
            ClassifyViewController *class = [[ClassifyViewController alloc]init];
            class.kindString = self.kindArray[indexPath.row];
            class.itemString = self.itemArray[indexPath.row];
            [viewController.navigationController pushViewController:class animated:YES];
            [class release];
            self.navigationController.navigationBarHidden = NO;
        }else if (indexPath.row == 1){
            CookingViewController *cook = [[CookingViewController alloc]init];
            cook.leftString = self.itemArray[indexPath.row];
            cook.urlString = self.kindArray[indexPath.row];
            [viewController.navigationController pushViewController:cook animated:YES];
            [cook release];
            self.navigationController.navigationBarHidden = NO;
            
        }else if (indexPath.row == 4){
            SeasonViewController *season = [[SeasonViewController alloc]init];
            season.leftString = self.itemArray[indexPath.row];
            season.urlString = self.kindArray[indexPath.row];
            [viewController.navigationController pushViewController:season animated:YES];
            [season release];
            self.navigationController.navigationBarHidden = NO;
        }else if (indexPath.row == 5){
            MovieViewController *movie = [[MovieViewController alloc]init];
            [viewController.navigationController pushViewController:movie animated:YES];
            
            [movie release];
            
            self.navigationController.navigationBarHidden = NO;
            
        }else if (indexPath.row == 6){
            
            MoreViewController *more = [[MoreViewController alloc]init];
            
            [self.navigationController pushViewController:more animated:YES];
            
            [more release];
        }

    
   
    
    };

}

-(void)createCarouseView:(NSMutableArray *)array
{
    ScrollView *scrol = [[ScrollView alloc] initWithFrame:CGRectMake(0, kHeight / 2, kWidth, kHeight / 2) array:array];
    
    //跳转到专题页
    scrol.block = ^(NSInteger index)
    {
        TopicViewController *topic = [[TopicViewController alloc]init];
        
//        [self.navigationController setNavigationBarHidden:NO];
        CarouselModel *carouse = self.carouseArray[index];
        topic.Id = carouse.Id;
        
        [self.navigationController pushViewController:topic animated:YES];
        [topic release];
    };
    
    scrol.autoresizesSubviews = NO;
    
    [self.view addSubview:scrol];
    [scrol release];
    [self.view sendSubviewToBack:scrol];

}

//收回抽屉
-(void)tap:(UITapGestureRecognizer *)tap
{if (self.drawerView.frame.origin.x == 0) {
    
    [UIView animateWithDuration:0.5 animations:^{
        self.drawerView.frame = CGRectMake(-kWidth / 5 * 4, 0,kWidth / 5 * 4, kHeight);
       
       
    }completion:^(BOOL finished) {
        self.shelterView.frame = CGRectMake(0, 0, 0, 0);
    }];
}
}

-(void)swipe:(UISwipeGestureRecognizer *)swipe
{  [self.view insertSubview:self.shelterView belowSubview:self.drawerView];
    self.shelterView.frame = self.view.frame;
   
    if (self.drawerView.frame.origin.x == -kWidth / 5 * 4) {
        [UIView animateWithDuration:0.5 animations:^{
            //    self.drawerView = [[DrawerView alloc]initWithFrame:CGRectMake(-kWidth / 5 * 4, 0, kWidth / 5 * 4, kHeight)];
            //self.drawerView.backgroundColor = [UIColor clearColor];
        self.drawerView.frame = CGRectMake(0, 0,kWidth / 5 * 4, kHeight);
            NSLog(@"ne");
        }];
    }
    
}

-(void)swipeAction:(UISwipeGestureRecognizer *)swipeOne
{
//    if (self.drawerView.frame.origin.x == 0) {
        [UIView animateWithDuration:0.5 animations:^{
        self.drawerView.frame = CGRectMake(-kWidth / 5 * 4, 0,kWidth / 5 * 4, kHeight);
            
    }completion:^(BOOL finished) {
        self.shelterView.frame = CGRectMake(0, 0, 0, 0);
    }];
//    }
    
    
    
    
    
}
/**********************  状态栏隐藏  **********************/

//	- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent;
//}

- (BOOL)prefersStatusBarHidden{
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
